package com.laihui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;

import com.laihui.MyApplication;
import com.laihui.adapters.ArticleAdapter;
import com.laihui.base.BaseActivity;
import com.laihui.entity.ArticleList;
import com.laihui.view.TopBar;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by chensk on 2017/12/26.
 */

public class PlatformAnnouncementActivity extends BaseActivity {
    @BindView(R.id.easy_recycler_view)
    EasyRecyclerView easyRecyclerView;
    private Activity mActivity;
    private MyApplication application;
    private ArticleAdapter adapter;
    //当前页数
    private int pageNum = 1;
    //每一页显示的行数
    private int epage = 10;
    private String mParam1;
    private static final String ARG_PARAM1 = "param1";
    private static final String TAG = "InvestRecordFragment";

    @BindView(R.id.topbar)//topbar
            TopBar topbar;
    @Override
    public void init() {
        mActivity =this;
        application = (MyApplication) mActivity.getApplication();
        easyRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new ArticleAdapter(mActivity);
        easyRecyclerView.setAdapter(adapter);
        getPlatformAnnouncement(false);
        easyRecyclerView.setErrorView(R.layout.error_layout);

        topbar.setOnTopbarClickListener(new TopBar.topbarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void rightClick() {

            }
        });

        easyRecyclerView.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNum = 1;
                getPlatformAnnouncement(true);
            }
        });
        adapter.setMore(R.layout.load_more_layout, new RecyclerArrayAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                pageNum++;
                getPlatformAnnouncement(false);
            }
        });
        adapter.setNoMore(R.layout.no_more_layout);
        adapter.setError(R.layout.error_layout);
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(mActivity, PlatformAnnouncementDetailActivity.class);
                intent.putExtra("article", adapter.getItem(position));
                startActivity(intent);

            }
        });
    }

    private void getPlatformAnnouncement(final boolean isRefresh) {
        Map<String, String> map = new HashMap<>();
        map.put("currentPage", pageNum + "");
        map.put("cate_name", getIntent().getStringExtra("cate_name"));
        application.apiService.articlelist(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ArticleList>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ArticleList articleList) {

                        if (isRefresh) {
                            adapter.clear();
                            adapter.addAll(articleList.list);
                        } else {
                            adapter.addAll(articleList.list);
                        }
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_platform_announcement;
    }

    @Override
    public Activity bindActivity() {
        return this;
    }
}
